<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>13.v-for循环中key的作用</title>
    <script src="./lib/vue-2.6.12.js"></script>
</head>

<body>
    <div id="app">
        <label for="">id:</label> <input type="text" v-model="id">
        <label for="">name:<input type="text" v-model="name"></label>
        <button @click="add">添加</button>
        <hr>
        <!-- key 可以在增删数据时，按照key来保证相关元素的状态不变，否则是按照idx来保持元素状态 -->
        <p v-for="item in list" :key="item.id">
            <input type="checkbox"> {{item.id}}, {{item.name}}
        </p>
    </div>

    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                id: 0,
                name: '',
                list: [
                    { id: 1, name: '李思' },
                    { id: 2, name: '嬴政' },
                    { id: 3, name: '赵高' },
                    { id: 4, name: '韩非' },
                    { id: 5, name: '荀子' }
                ]
            },
            methods: {
                add() {
                    this.list.unshift({ id: this.id, name: this.name });
                }
            }
        })        
    </script>
</body>

</html>